/**
 * mustions 使用
 * store.commit(type, payload)
 * 调用：
 * mustions[type](state, payload)
 */

import { computed } from 'vue';

export function createMutations(store, mutations) {
  Object.keys(mutations).forEach((key) => {
    store._mutations[key] = (payload) => mutations[key](store.state, payload);
  });
}

/**
 * 调用：
 */
export function createActions(store, actions) {
  Object.keys(actions).forEach((key) => {
    store._actions[key] = (payload) => actions[key](store, payload);
  });
}

export function createGetters(store, getters) {
  store.getters = {};
  Object.keys(getters).forEach((key) => {
    Object.defineProperty(store.getters, key, {
      get: () => getters[key](store.state, store.getters),
      // get: () => computed(getters[key](store.state, getters)).value,
    });
  });
}

export function createCommit(store, commit) {
  store.commit = function (type, payload) {
    commit.apply(store, [type, payload]);
  };
}
export function createDisaptch(store, dispatch) {
  store.dispatch = function (type, payload) {
    dispatch.apply(store, [type, payload]);
  };
}
